#include "inputCtl.h"
#include "chargeCtrl.h"	//extern timeoutCalc(uint32_t,uint32_t)



typedef struct{
	uint8_t keyCount;			//cycle cout
	
}inputParameter_t;



inputParameter_t inputParameterDef;
 


inputDetection_t    inputDetectionDef[] = {
	
	//Evnet ID      |                    IO Status  | short Flag  | long Flag  | short Time      | long time   	   | GPIO pin    			   | GPIO group			      | short press call back | longcalback function
	{		EMERGENCYSTOP_STATUS,				0,			0,			0,				50,			3000,			EMERGENCYSTOP_Pin,			EMERGENCYSTOP_Port,			ShortPressCallback,		longPressCallback},
	{		AC_CONTACT_STATUS,					0,			0,			0,				50,			3000,			AC_CONTACTOR_Pin,			AC_CONTACTOR_Port,			ShortPressCallback,		longPressCallback},
	{		AC_BREAK_STATUS,					0,			0,			0,				50,			3000,			AC_BREAK_Pin,				AC_BREAK_Port,				ShortPressCallback,		longPressCallback},
	{		LIGHTNINGARRESTER_STATUS,			0,			0,			0,				50,			3000,			LIGHTNIGARRESTER_Pin,		LIGHTNIGARRESTER_Port,		ShortPressCallback,		longPressCallback},
	{		FANFEEDBACK_STATUS,					0,			0,			0,				50,			3000,			FANFEEDBACK_Pin,			FANFEEDBACK_Port,			ShortPressCallback,		longPressCallback},
	{		ACCESSCONTROLSWITCH_STATUS,			0,			0,			0,				50,			3000,			ACCESSCONTROLSWITCH_Pin,	ACCESSCONTROLSWITCH_Port,	ShortPressCallback,		longPressCallback},
	{		SMOKESENSOR_STATUS,					0,			0,			0,				50,			3000,			SMOKESENSOR_Pin,			SMOKESENSOR_Port,			ShortPressCallback,		longPressCallback},
	{		AGUNDCCONTACTFIRST_STATUS,			0,			0,			0,				50,			3000,			AGUNDCCONTACTFIRST_Pin,		AGUNDCCONTACTFIRST_Port,	ShortPressCallback,		longPressCallback},
	{		AGUNDCCONTACTSECOND_STATUS,			0,			0,			0,				50,			3000,			AGUNDCCONTACTSECOND_Pin,	AGUNDCCONTACTSECOND_Port,	ShortPressCallback,		longPressCallback},
	{		AGUNDCCONTACTTHIRD_STATUS,			0,			0,			0,				50,			3000,			AGUNDCCONTACTTHIRD_Pin,		AGUNDCCONTACTTHIRD_Port,	ShortPressCallback,		longPressCallback},
	{		AGUNELECTRONICLOCK_STATUS,			0,			0,			0,				50,			3000,			AGUNDELECTRONICLOCK_Pin,	AGUNDELECTRONICLOCK_Port,	ShortPressCallback,		longPressCallback},
	{		AGUNDCCONTACTFUSES_STATUS,			0,			0,			0,				50,			3000,			AGUNDCCONTACTFUSES_Pin,		AGUNDCCONTACTFUSES_Port,	ShortPressCallback,		longPressCallback},
	{		BGUNDCCONTACTFIRST_STATUS,			0,			0,			0,				50,			3000,			BGUNDCCONTACTFIRST_Pin,		BGUNDCCONTACTFIRST_Port,	ShortPressCallback,		longPressCallback},
	{		BGUNDCCONTACTSECOND_STATUS,			0,			0,			0,				50,			3000,			BGUNDCCONTACTSECOND_Pin,	BGUNDCCONTACTSECOND_Port,	ShortPressCallback,		longPressCallback},
	{		BGUNDCCONTACTTHIRD_STATUS,			0,			0,			0,				50,			3000,			BGUNDCCONTACTTHIRD_Pin,		BGUNDCCONTACTTHIRD_Port,	ShortPressCallback,		longPressCallback},
	{		BGUNELECTRONICLOCK_STATUS,			0,			0,			0,				50,			3000,			BGUNDELECTRONICLOCK_Pin,	BGUNDELECTRONICLOCK_Port,	ShortPressCallback,		longPressCallback},
	{		BGUNDCCONTACTFUSES_STATUS,			0,			0,			0,				50,			3000,			BGUNDCCONTACTFUSES_Pin,		BGUNDCCONTACTFUSES_Port,	ShortPressCallback,		longPressCallback},
	{		AGUN_CHADEMO_J_STATUS,				0,			0,			0,				50,			3000,			A_CHADEMO_J_Pin,			A_CHADEMO_J_Port,			ShortPressCallback,		longPressCallback},
	{		AGUN_CHADEMO_PD_STATUS,				0,			0,			0,				50,			3000,			A_CHADEMO_PD_Pin,			A_CHADEMO_PD_Port,			ShortPressCallback,		longPressCallback},
	{		AGUN_CHADEMO_LOCK_STATUS,			0,			0,			0,				50,			3000,			A_IN__CHADEMO_LOCK_Pin,		A_IN_CHADEMO_LOCK_Port,		ShortPressCallback,		longPressCallback},
	{		BGUN_CHADEMO_J_STATUS,				0,			0,			0,				50,			3000,			B_CHADEMO_J_Pin,			B_CHADEMO_J_Port,			ShortPressCallback,		longPressCallback},
	{		BGUN_CHADEMO_PD_STATUS,				0,			0,			0,				50,			3000,			B_CHADEMO_PD_Pin,			B_CHADEMO_PD_Port,			ShortPressCallback,		longPressCallback},
	{		BGUN_CHADEMO_LOCK_STATUS,			0,			0,			0,				50,			3000,			B_IN_CHADEMO_LOCK_Pin,		B_IN_CHADEMO_LOCK_Port,		ShortPressCallback,		longPressCallback},	
	
	
};

void inputDetectionInit(void)
{
	
	inputParameterDef.keyCount = EMERGENCYSTOP_STATUS;
}



__weak void ShortPressCallback(void)
{
	//do nothing
}

__weak void longPressCallback(void)
{
	//do nothing
}


void inputDetectionPorcess(void)
{
	uint8_t i = 0;
	static uint8_t TickCount[32] = {0x00};
	
	for(i = 0; i < INPUT_DETECTION_MAX; i ++)
	{
		inputDetectionDef[i].KeyStatus = HAL_GPIO_ReadPin(inputDetectionDef[i].GPIOx, inputDetectionDef[i].Pin);
				
		if(inputDetectionDef[i].KeyStatus)//relay disconnection
		{
			//short press
			if(timeoutCalc(TickCount[i],inputDetectionDef[i].shortDebounceTicks))
			{
				TickCount[i] = HAL_GetTick();
				inputDetectionDef[i].shortPressFlag = 1;
				if(inputDetectionDef[i].shortPresscallback != NULL)
				{
					inputDetectionDef[i].shortPresscallback();
				}
			}
			//long press
			if(timeoutCalc(TickCount[i],inputDetectionDef[i].longDebounceTicks))
			{
				inputDetectionDef[i].longPressFlag = 1; 
				if(inputDetectionDef[i].longPresscallback != NULL)
				{
					inputDetectionDef[i].longPresscallback();
				}
			}
		}
		else//relay close
		{
			TickCount[i] = HAL_GetTick();
			inputDetectionDef[i].shortPressFlag = 0; 
		}
	}
}
		

uint8_t getShortPressSignal(inputCtlStatus_e eventID)
{
	return inputDetectionDef[eventID].shortPressFlag;
}

		

uint8_t getlongPressSignal(inputCtlStatus_e eventID)
{	
	return inputDetectionDef[eventID].longPressFlag;
}



